package com.phuc.game.andPuzzle;

import java.util.ArrayList;

import com.phuc.game.andPuzzle.common.Utils;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LevelAdapter extends BaseAdapter {
	private ArrayList<LevelObject> mListObjects;
	private final Activity mContext;
	
	static class ViewHolder {
		public TextView tv_levelNumber;
		public ImageView imgView;
		public ImageView imgStar;
		public LinearLayout rowBack;
	}
	
	public LevelAdapter(Activity context, ArrayList<LevelObject> listObject) {
		mContext = context;
		mListObjects = listObject;
	}

	@Override
	public int getCount() {
		return mListObjects.size();
	}

	@Override
	public Object getItem(int indext) {
		return mListObjects.get(indext);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		String str_position = Integer.toString(position);
		View rowView = convertView;
//		if(rowView == null){
		   LayoutInflater inflater = mContext.getLayoutInflater();
		   rowView = inflater.inflate(R.layout.row_item, null);
		   ViewHolder viewHolder = new ViewHolder();
		   viewHolder.tv_levelNumber = (TextView) rowView
				.findViewById(R.id.tv_levelNumber);
           viewHolder.imgView = (ImageView)rowView.findViewById(R.id.img);
           viewHolder.imgStar = (ImageView)rowView.findViewById(R.id.img_levelStart);
           viewHolder.rowBack = (LinearLayout)rowView.findViewById(R.id.item);
           rowView.setTag(viewHolder);
//		}
		
		ViewHolder holder = (ViewHolder)rowView.getTag();
		holder.tv_levelNumber.setText("Hot Girl " + Integer.toString(position+1));
		
		
		if (position < MainActivity.points.length) {
			Utils.loadImageAssets(mContext, holder.imgView, str_position);
			if (MainActivity.points[position] == 1) {
				holder.imgStar.setImageResource(R.drawable.star_1);
			} else if (MainActivity.points[position] == 2) {
				holder.imgStar.setImageResource(R.drawable.star_2);
			} else if (MainActivity.points[position] == 3) {
				holder.imgStar.setImageResource(R.drawable.star_3);
			} 
		} else {
			holder.imgView.setImageResource(R.drawable.unknown_icon);
			holder.imgStar.setImageResource(R.drawable.star_0);
			if (position > MainActivity.points.length) {
				viewHolder.rowBack.setBackgroundResource(R.drawable.background_click);
			}
		}
		
		return rowView;
	}

}
